package 牛客网_N.动态规划.minimun_path_sum_8;

import java.util.Scanner;

/**
 * 机器人走格子进阶
 * @author xuliyun
 * @summary 左上到右下，求最小和。
 */
public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int m = Integer.parseInt(in.nextLine());
		int n = Integer.parseInt(in.nextLine());
		int[][] grid = new int[m][n];
		for(int i = 0; i < m; i++){
			String str = in.nextLine();
			String[] dataStr = str.split(" ");
			for(int j = 0; j < n; j++){
				grid[i][j] = Integer.parseInt(dataStr[j]);
			}
		}
		System.out.println(minPathSum(grid));
	}
	
	public static int minPathSum(int[][] grid){
		int[][] res = new int[grid.length][grid[0].length];
		res[0][0] = grid[0][0];
		for(int i = 1; i < grid.length; i++){
			res[i][0] = res[i-1][0]+grid[i][0];
		}
		for(int i = 1; i < grid[0].length; i++){
			res[0][i] = res[0][i-1]+grid[0][i];
		}
		for(int i = 1; i < grid.length; i++){
			for(int j = 1; j < grid[0].length; j++){
				res[i][j] = Math.min(res[i-1][j]+grid[i][j], res[i][j-1]+grid[i][j]);
			}
		}
		return res[grid.length-1][grid[0].length-1];
	}
}
